package com.hy;

/**
 * Created With IntelliJ IDEA.
 * Descriptions:无重复字符的最长子串
 * author: Mr.Du
 * Date: 2022/11/14
 * Time: 21:53
 */
public class LengthOfLongestSubstring {
    
    public static void main(String[] args) {
        LengthOfLongestSubstring lengthOfLongestSubstring = new LengthOfLongestSubstring();
        int res = lengthOfLongestSubstring.lengthOfLongestSubstring("pwwkew");
        System.out.println(res);
    }
    
    /**
     * 滑动窗口：快慢指针判断
     * @param s
     * @return
     */
    public int lengthOfLongestSubstring(String s) {
        int[] arr = new int[256];
        int size = s.length();
        int res = 0;
        int slow = 0;
        int fast = 0;
        while(fast < size){
            int index = s.charAt(fast) - 0;
            arr[index] += 1;
            fast++;
            // 有重复，就移动left
            while(arr[index] > 1){
                int index1 = s.charAt(slow) - 0;
                slow++;
                arr[index1] -= 1;
            }
            res = Math.max(res,fast - slow);
        }
        return res;
    }
}
